﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

//未划分模块的消息解析器
public static  class CmdPaser
{
    public static void OnLogin(Cmd cmd)
    {
        // Debug.Log("消息分发成功");
        //cmd的类型必须是LoginCmd
        if (!Net.CheckCmd(cmd,typeof(LoginCmd)))
        {
            return;
        }
        LoginCmd loginCmd = cmd as LoginCmd;
        //验证账号密码
        //找到玩家的存档
        Server.instance.CurPlayer  = new Player();
        var player = Server.instance.CurPlayer;
        //向客户端发送玩家的以创建的角色列表
        RoleListCmd roleListCmd = new RoleListCmd();
        //roleListCmd.AllRole = player.AllRole.GetRange(0, player.AllRole.Count);
        foreach(var role in player.AllRole)
        {
            var roleInfo = new SelectRoleInfo() { Name = role.Name, ModelID = role.ModelID };
            roleListCmd.AllRole.Add(roleInfo);
        }
        Server.instance.SendCmd(roleListCmd);
      
    }

    internal static void OnSelectRole(Cmd cmd )
    {
        if (!Net.CheckCmd(cmd, typeof(SelectRoleCmd)))
        {
            return;
        }
        SelectRoleCmd selectRoleCmd = cmd as SelectRoleCmd;
        var curPlayer = Server.instance.CurPlayer;
        SelectRoleInfo curRole = curPlayer.AllRole[selectRoleCmd.Index];

        //告诉客户端，场景编号
        var sceneID = 1;
        EnterMapCmd enterMapCmd = new EnterMapCmd() { MapID = sceneID };
        //分配ThisID
        var thisid = RoleServer.GetNewThisID();
        //告诉客户端，主角的ThisID
        MainRoleThisIDCmd mainRoleThisIDCmd = new MainRoleThisIDCmd() { ThisID = thisid };
        //Server.instance.SendCmd(mainRoleThisIDCmd);
        //生成主角
        CreateSceneRoleCmd roleCmd = new CreateSceneRoleCmd();
        roleCmd.ThisID = thisid;
        roleCmd.Name = curRole.Name;
        roleCmd.ModleID = curRole.ModelID;
        roleCmd.pos = Vector3.zero;
        roleCmd.FaceTo = Vector3.forward;

        //发送消息
        Server.instance.SendCmd(enterMapCmd);
        Server.instance.SendCmd(mainRoleThisIDCmd);
        Server.instance.SendCmd(roleCmd);
        //生成附近的配角（不考虑）
        //生成附近的Npc
        //CreateSceneRole
    }
}
